<template>
    <div class="payment">
        <!--搜索header-->
        <div class="header_goods">
            <img @click="goBack()" class="back" src="../../assets/img/goods/back.png" alt="">
            <img class="more" src="../../assets/img/goods/more.png" alt="">
        </div>
        <div style="margin: 15%">
            <div>
                <p><label>支付帐号:</label>{{payAccount}}</p>
                <p><label>支付渠道:</label>{{payChannel}}</p>
                <p><label>支付单号:</label>{{payOrder}}</p>
                <p><label>支付金额:</label>{{payAmount}}</p>
            </div>
            <input type="button" name="paySubmit" value="支付" @click="goPay()"/>
        </div>
    </div>
</template>

<style>
    @import url("./payment.css");
    @import url('../../assets/css/swiper.css');
</style>
<script>
    import {Toast} from 'mint-ui';

    export default {
        data() {
            return {
                payUrl: "http://demo.dcloud.net.cn/payment/?payid=",
                channelObj: {},
                payChannel: "支付宝",//支付渠道
                payAccount: "Harrison_han@163.com",//支付帐号
                payOrder: "2017092303423",//支付单号
                payAmount: 1 //支付金额
            }
        },
        created() {
             window.scrollTo(0,0);
            document.addEventListener('plusready', this.plusReady(), false);
        },
        methods: {
            plusReady() {
                let _this = this;
                plus.payment.getChannels(function (channels) {
                    for (var i in channels) {
                        var channel = channels[i];
                        _this.channelObj[channel.id] = channel;
                    }
                    _this.checkPayService(_this.channelObj['alipay']);
                }, function (e) {
                    console.log('获取支付通道失败：' + e.message);
                });
            },
            checkPayService(channel) {
                if (!channel.serviceReady) {
                    var txt = null;
                    switch (channel.id) {
                        case 'alipay':
                            txt = '检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？';
                            break;
                        default:
                            txt = '系统未安装“' + channel.description + '”服务，无法完成支付，是否立即安装？';
                            break;
                    }
                    plus.nativeUI.confirm(txt, function (e) {
                        if (e.index == 0) {
                            channel.installService();
                        }
                    }, channel.description);
                }
            },
            goBack() {
                this.$router.goBack()
            },
            goPay() {
                let requestUrl = this.payUrl;
                var appid=plus.runtime.appid;
                if(navigator.userAgent.indexOf('StreamApp')>=0){
                    appid='Stream';
                }
                let channel = this.channelObj['alipay'];
                if (channel.id == 'alipay') {
                    requestUrl = requestUrl + channel.id+'&appid='+appid+'&total=1';
                    let waiting = plus.nativeUI.showWaiting();
                    let xhr = new XMLHttpRequest();
                    let _this = this;
                    xhr.onreadystatechange = function () {
                        switch (xhr.readyState) {
                            case 4:
                                waiting.close();
                                waiting = null;
                                let aa = "alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2017082708412835&biz_content=%7B%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%222017090700003%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22App%E6%94%AF%E4%BB%98%E6%B5%8B%E8%AF%95Java%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%220.01%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2F181j970a19.imwork.net%3A28204%2Frest%2Forder%2Fpay%2Falipay%2Fnotify&sign=RWUb7XHQY7uL87%2BIH3%2BQgwHYbxedTsm1G0gU%2BDfGtfZnABWK38OuwkryXQLgEPNuv%2BkKUbQCBbgHc1orD5KD3xUqlEUwO94rt7Bq5E2VzrKY2hcrMAc8OwQO8fuzB5ra%2F%2FUTuexuOdBEv3KU4BpaJxSTozJ96ZWNiAk2XK6bvv4%3D&sign_type=RSA&timestamp=2017-09-07+18%3A10%3A10&version=1.0";
                                if (xhr.status == 200) {
                                    plus.payment.request(channel, aa, function (result) {
                                        console.log(JSON.stringify(result));
                                        plus.nativeUI.alert("支付成功！", function () {
                                            back();
                                        });
                                    }, function (error) {
                                        let msg = '';
                                        switch (error.code) {
                                            case 62000:
                                                msg = '客户端未安装支付通道依赖的服务';
                                                break;
                                            case 62001:
                                                msg = '用户取消支付操作';
                                                break;
                                            case 62002:
                                                msg = '此设备不支持支付';
                                                break;
                                            case 62003:
                                                msg = '数据格式错误';
                                                break;
                                            case 62004:
                                                msg = '支付账号状态错误';
                                                break;
                                            case 62005:
                                                msg = '订单信息错误';
                                                break;
                                            case 62006:
                                                msg = '支付操作内部错误';
                                                break;
                                            case 62007:
                                                msg = '支付服务器错误';
                                                break;
                                            case 62008:
                                                msg = '网络问题引起的错误';
                                                break;
                                            default:
                                                msg = '其它未定义的错误';
                                                break;
                                        }
                                        plus.nativeUI.alert(error.code + ":" + msg);
                                    });
                                } else {
                                    plus.nativeUI.alert("获取订单信息失败！status="+xhr.status, null, "提示:");
                                    return;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    xhr.open('GET', requestUrl);
                    xhr.send();
                } else {
                    plus.nativeUI.alert("不支持此支付通道！", null, "提示:");
                    return;
                }
            },

        }
    }
</script>
